/*
 * Created by JFormDesigner on Wed Nov 05 22:18:22 CET 2014
 */

package pl.PawelAndAlicja.badugiproject.main.Gui;

import pl.PawelAndAlicja.badugiproject.main.Server.Server;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author Pawel Linkshell
 */
public class NewGameJDialog extends JDialog {
    public NewGameJDialog(ClientGUI owner) {
        super(owner,  owner.getPlayer().getName(), true);
        this.father = owner;
        initComponents();
        okButton.setEnabled(false);
    }

//    public NewGameJDialog(Dialog owner) {
//        super(owner);
//        initComponents();
//    }

    public JTextField getBigBlindTextField() {
        return bigBlindTextField;
    }

    public JTextField getSmallBlindTextField() {
        return smallBlindTextField;
    }

    public JTextField getTableNameTextField() {
        return tableNameTextField;
    }



    private void okButtonActionPerformed(ActionEvent e) {
        String ae = e.paramString();
        System.out.println(ae);

//        father.setSettingsClient(father.getTitle() + "  " + tableNameTextField.getText(), getSmallBlindTextField().getText(), getBigBlindTextField().getText());
        Server.setTableName(tableNameTextField.getText());
        Server.setSmallBlindAmounts(Integer.parseInt(smallBlindTextField.getText()));
        Server.setBigBlindAmounts(Integer.parseInt(bigBlindTextField.getText()));

        father.setTitle(father.getTitle() + "  " + Server.getTableName());
        dispose();
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        String ae = e.paramString();
        System.out.println(ae);
        System.out.println("Pressed CANCEL button");
        dispose();

    }

    private void searchTableActionPerformed(ActionEvent e) {
        String ae = e.paramString();
        System.out.println(ae);

        createServer();

        okButton.setEnabled(true);
        searchButton.setVisible(false);

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        this.setSize(screenWidth / 5, screenHeight / 4);
    }

    private void createServer() {
        if (!Server.isServerReady()) {
            Server.getInstance();
            System.out.println("INFO: Server Succesfully switch ON");
            JOptionPane.showMessageDialog(null, "Utworzylem nowy stół");
            father.setTitle(father.getTitle() + " [SERVER + CLIENT]");
            newTable.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "Znalazlem stół");
            father.setTitle(father.getTitle() + "[CLIENT]");
        }
    }

    public JPanel getNewTable() {
        return newTable;
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Pawel Linkshell
        JPanel dialogPane = new JPanel();
        JPanel contentPanel = new JPanel();
        searchButton = new JButton();
        newTable = new JPanel();
        JLabel tableNameLabel = new JLabel();
        tableNameTextField = new JTextField();
        JLabel smallBlindLabel = new JLabel();
        smallBlindTextField = new JTextField();
        JLabel bigBlindLabel = new JLabel();
        bigBlindTextField = new JTextField();
        JPanel buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

            // JFormDesigner evaluation mark
            dialogPane.setBorder(new javax.swing.border.CompoundBorder(
                new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
                    "JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
                    javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
                    java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new GridBagLayout());
                ((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0};
                ((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0};
                ((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};
                ((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

                //---- searchButton ----
                searchButton.setText("Szukaj sto\u0142u");
                searchButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        searchTableActionPerformed(e);
                    }
                });
                contentPanel.add(searchButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //======== newTable ========
                {
                    newTable.setBorder(new TitledBorder("Nowy St\u00f3\u0142"));
                    newTable.setVisible(false);
                    newTable.setLayout(new GridBagLayout());
                    ((GridBagLayout)newTable.getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0};
                    ((GridBagLayout)newTable.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
                    ((GridBagLayout)newTable.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};
                    ((GridBagLayout)newTable.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};

                    //---- tableNameLabel ----
                    tableNameLabel.setText("Nazwa stolu:");
                    newTable.add(tableNameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 5), 0, 0));

                    //---- tableNameTextField ----
                    tableNameTextField.setText("Moj Super stol");
                    newTable.add(tableNameTextField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 5), 0, 0));

                    //---- smallBlindLabel ----
                    smallBlindLabel.setText("Stawka \"Ma\u0142a Ciemna\":");
                    newTable.add(smallBlindLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 5), 0, 0));

                    //---- smallBlindTextField ----
                    smallBlindTextField.setText("100");
                    newTable.add(smallBlindTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 5), 0, 0));

                    //---- bigBlindLabel ----
                    bigBlindLabel.setText("Stawka \"Du\u017ca Ciemna\":");
                    newTable.add(bigBlindLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                    //---- bigBlindTextField ----
                    bigBlindTextField.setText("200");
                    newTable.add(bigBlindTextField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));
                }
                contentPanel.add(newTable, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("OK");
                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        okButtonActionPerformed(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cancelButtonActionPerformed(e);
                    }
                });
                buttonBar.add(cancelButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.NORTH);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Pawel Linkshell
    private JButton searchButton;
    private JPanel newTable;
    private JTextField tableNameTextField;
    private JTextField smallBlindTextField;
    private JTextField bigBlindTextField;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    private final ClientGUI father;



}
